package com.java.zhuiyun.api.memberShip_records.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.zhuiyun.api.alipay.controller.AliPay;
import com.java.zhuiyun.api.home.model.ModelOrder;
import com.java.zhuiyun.api.memberShip_records.entity.UserMemberShip;
import com.java.zhuiyun.api.memberShip_records.model.MemberShipModel;
import com.java.zhuiyun.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @InterfaceName MemberShipMapper
 * @Description TODO
 * @Author WX
 * @Date 2023/8/18 8:55
 **/
@Mapper
public interface MemberShipMapper extends BaseMapper<MemberShipModel> {
    /*
     * @Author WX
     * @Description 生成订单
     * @Date 2023/8/18 9:01
     * @Return
     * @param UserMemberShip
     */
    @Insert("INSERT INTO user_member_ship (member_number, user_id, member_id, pay_price, expiry_time, create_time, update_time) " +
            "VALUES (#{memberNumber}, #{userId}, #{memberId}, #{payPrice}, #{expiryTime}, #{createTime}, #{updateTime})")
    int generateOrder(UserMemberShip userMemberShip);

    /*
     * @Author WX
     * @Description 查询个人会员购买记录
     * @Date 2023/8/18 13:57
     * @Return
     * @param id
     */
    @Select("select * from user_member_ship s,member_type t where s.user_id = #{id} and s.member_id = t.id ")
    List<MemberShipModel> personInfoQuery(Integer id);

    /*
    * @Author WX
    * @Description 获取对象中的用户id
    * @Date 2023/8/23 11:23
    * @Return
    * @param null
    */
    @Select("select * from user_member_ship where user_id =#{codeId}")
    List<UserMemberShip> findByUserId(Integer codeId);

    /*
    * @Author WX
    * @Description 依据订单编号来查找订单
    * @Date 2023/8/23 11:23
    * @Return
    * @param memberNumber
    */
    @Select("SELECT * FROM user_member_ship WHERE member_number = #{memberNumber} ")
    AliPay selectOneShip(String memberNumber);

    /*
    * @Author WX
    * @Description 修改对应的订单编号的数据
    * @Date 2023/8/23 11:25
    * @Return
    * @param null
    */
    @Update("UPDATE user_member_ship " +
            "SET payment_code = #{paymentCode}, " +
            "create_time = #{createTime}, " +
            "payment = #{payment},expiry_time = #{expiryTime}  " +
            "WHERE id = #{id}")
    int updateById(AliPay aliPay);

    /*
     * @Author WX
     * @Description 查询所有会员记录
     * @Date 2023/8/28 11:23
     * @Return
     * @param null
     */
    @Select("SELECT * FROM user_member_ship m, front_desk_user f, member_type t WHERE m.user_id = f.id AND m.member_id = t.id AND t.id != 4 AND m.payment = 1 AND MONTH(m.create_time) = MONTH(CURRENT_DATE())")
    List<MemberShipModel> selectMemberShip();

    /*
     * @Author WX
     * @Description 查询各种会员本月购买记录
     * @Date 2023/8/28 14:20
     * @Return
     * @param null
     */
    // 当前月
    // @Select("<script>SELECT * FROM user_member_ship m, member_type t WHERE m.member_id = t.id AND m.member_id IN <foreach item='item' collection='memberIds' open='(' separator=',' close=')'>#{item}</foreach> AND m.payment = 1 AND MONTH(m.create_time) = MONTH(CURRENT_DATE())</script>")
    // 当前季度
    //    @Select("<script>SELECT * " +
    //            "FROM user_member_ship m, member_type t " +
    //            "WHERE m.member_id = t.id " +
    //            "AND m.member_id IN ( " +
    //            "    <foreach item='item' collection='memberIds' open='(' separator=',' close=')'>#{item}</foreach> " +
    //            ") " +
    //            "AND m.payment = 1 " +
    //            "AND QUARTER(m.create_time) = QUARTER(CURDATE())</script>")
    // 所有
    @Select("<script>SELECT * FROM user_member_ship m, member_type t WHERE m.member_id = t.id AND m.member_id IN <foreach item='item' collection='memberIds' open='(' separator=',' close=')'>#{item}</foreach> AND m.payment = 1 </script>")
    List<MemberShipModel> getMembers(@Param("memberIds") List<Integer> memberIds);


    /*
     * @Author WX
     * @Description 当年会员每月销量
     * @Date 2023/9/7 10:17
     * @Return
     * @param null
     */
    @Select("SELECT MONTH(create_time) AS month,SUM(pay_price) AS price,COUNT(*) AS prices FROM user_member_ship WHERE payment = 1 AND del_flag = 0 AND YEAR(create_time) = #{selectedYear} AND MONTH(create_time) = #{mounth}")
    ModelOrder getMemberMonth(String mounth,String selectedYear);
}
